import 'package:flutter/cupertino.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Widget/ChooserBox.dart';
import 'package:hitutor/Widget/MyDialog.dart';
import 'package:hitutor/Widget/MyImage.dart';
import 'package:hitutor/Widget/MyInput.dart';
import 'package:hitutor/Widget/MyPage.dart';
import 'package:hitutor/Widget/Star.dart';

import '../../../Util/HttpGo.dart';
import 'FavoriteTeacher.dart';

class FavoriteTeacherSearch extends StatefulWidget {
  List<dynamic> languageList = [];
  int languageIndex = -1;
  String searchKey = "";

  FavoriteTeacherSearch({required this.languageList, required this.languageIndex, required this.searchKey});

  @override
  State<StatefulWidget> createState() {
    return _FavoriteTeacherSearch();
  }
}

class _FavoriteTeacherSearch extends State<FavoriteTeacherSearch> {
  openLanguageChooser() {
    showWheelDialog2(context, "", widget.languageList, "languageName", (index) {
      chooseLanguage(index);
    });
  }

  chooseLanguage(int index) {
    widget.languageIndex = index;
    setState(() {});
    searchTeacher();
  }

  TextEditingController controller = TextEditingController();

  @override
  void initState() {
    controller.text = widget.searchKey;
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      searchTeacher();
    });
    super.initState();
  }

  List<dynamic> teacherList = [];

  searchTeacher() {
    var languageCode = widget.languageIndex >= 0 ? widget.languageList[widget.languageIndex]["languageCode"] : "";
    String teacherName = controller.text;
    HttpGo(context).get("/Teacher/GetTeacherList?languageCode=$languageCode&teacherName=$teacherName", {}, (data) {
      teacherList = data["teacherList"];
      setState(() {});
    }, (code, error) => showToast(error));
  }

  @override
  Widget build(BuildContext context) {
    String language = widget.languageIndex >= 0 ? widget.languageList[widget.languageIndex]["languageName"] : "";

    return MyPage(
        "搜尋教師",
        SingleChildScrollView(
            child: Container(
                padding: EdgeInsets.all(16),
                child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                  ChooserBox(language.isEmpty ? "選擇語言" : language, onTap: openLanguageChooser),
                  Container(
                      decoration: Global.getBg(MyColor.F2, 5),
                      padding: EdgeInsets.fromLTRB(4, 0, 8, 0),
                      margin: EdgeInsets.fromLTRB(0, 8, 0, 8),
                      height: 28,
                      child: Row(children: [
                        Expanded(
                            child: NormalInput(
                                controller: controller,
                                hintText: "輸入老師名字",
                                fontSize: 12,
                                onSubmit: (value) {
                                  searchTeacher();
                                })),
                        LocalImage("search_blue.png", width: 16)
                      ])),
                  ListView.builder(
                      itemCount: teacherList.length,
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemBuilder: (BuildContext context, int index) {
                        return ItemTeacher(teacherData: teacherList[index]);
                      }),
                ]))));
  }
}
